package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.CheckRecordDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.CheckRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckRecordService.class)
@Transactional
public class CheckRecordServiceImpl implements CheckRecordService {
    @Autowired
    private CheckRecordDao recordDao;
    @Override
    public Result findAll(String startDate,String endDate) {
        try {

            //判断参数是否为空
            if (startDate.equals("")||startDate.length()==0){
                startDate = null;
            }
            if (endDate.equals("")||endDate.length()==0){
                endDate = null;
            }
            //查询所有预约
            List<Order> orderList = recordDao.findAll(startDate,endDate);
            for (Order order : orderList) {
                String sex = order.getMember().getSex();
                if (sex!=null&&!sex.equals("0")&&sex.length()>0){
                    if (sex.equals("1")){
                        order.getMember().setSex("男");
                    }else {
                        order.getMember().setSex("女");
                    }
                }
            }
            return new Result(true,"查询成功",orderList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    @Override
    public Result findById(int id) {
        try {
            //根据id查询预约详情
            Order order = recordDao.findById(id);
            //根据id查询对应体检地址信息
            String address = recordDao.findAddress(id);
            order.setAddress(address);
            return new Result(true,"查询预约详情成功",order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询预约详情失败");
        }
    }
}
